package com.shop.zkmall.app.service.a.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.zkmall.app.dao.buyer.CereBuyerUserDAO;
import com.shop.zkmall.app.domain.CereBuyerBalanceDetail;
import com.shop.zkmall.app.domain.req.BalanceDetailReq;
import com.shop.zkmall.app.mapper.CereBuyerBalanceDetailMapper;
import com.shop.zkmall.app.service.a.ICereBuyerBalanceDetailService;
import com.shop.zkmall.app.utils.LoginHelper;
import com.shop.zkmall.app.utils.R;
import com.shop.zkmall.commons.domain.buyer.CereBuyerUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 余额明细表（用户资金流水） 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-11-25
 */
@Service
public class CereBuyerBalanceDetailServiceImpl extends ServiceImpl<CereBuyerBalanceDetailMapper, CereBuyerBalanceDetail> implements ICereBuyerBalanceDetailService {

    @Autowired
    private CereBuyerUserDAO cereBuyerUserService;

    @Override
    public void addBalanceDetail(CereBuyerUser cereBuyerUser, BigDecimal totalPrice, String remark) {
        CereBuyerBalanceDetail cereBuyerBalanceDetail = new CereBuyerBalanceDetail();
        cereBuyerBalanceDetail.setUserId(cereBuyerUser.getBuyerUserId());
        cereBuyerBalanceDetail.setChangeType(1);
        cereBuyerBalanceDetail.setAmount(totalPrice);
        cereBuyerBalanceDetail.setBeforeAmount(cereBuyerUser.getBalance());
        cereBuyerBalanceDetail.setAfterAmount(cereBuyerUser.getBalance().add(totalPrice));
        cereBuyerBalanceDetail.setRemark(remark);
        save(cereBuyerBalanceDetail);

    }

    @Override
    public void subtractBalanceDetail(CereBuyerUser cereBuyerUser, BigDecimal totalPrice, String s) {
        CereBuyerBalanceDetail cereBuyerBalanceDetail = new CereBuyerBalanceDetail();
        cereBuyerBalanceDetail.setUserId(cereBuyerUser.getBuyerUserId());
        cereBuyerBalanceDetail.setChangeType(0);
        cereBuyerBalanceDetail.setAmount(totalPrice);
        cereBuyerBalanceDetail.setBeforeAmount(cereBuyerUser.getBalance());
        cereBuyerBalanceDetail.setAfterAmount(cereBuyerUser.getBalance().add(totalPrice));
        cereBuyerBalanceDetail.setRemark(s);
        save(cereBuyerBalanceDetail);
    }

    @Override
    public synchronized R<BigDecimal> getBalance() {
        CereBuyerUser cereBuyerUser = cereBuyerUserService.selectByPrimaryKey(LoginHelper.getUserId());
        return R.ok(cereBuyerUser.getBalance());
    }

    @Override
    public R<?> balanceDetail(BalanceDetailReq param) {
        Page<CereBuyerBalanceDetail> objectPage = new Page<>(param.getPage(), param.getPageSize());
        LambdaQueryWrapper<CereBuyerBalanceDetail> cereBuyerBalanceDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cereBuyerBalanceDetailLambdaQueryWrapper.eq(CereBuyerBalanceDetail::getUserId, LoginHelper.getUserId());
        if (param.getType() !=2){
            cereBuyerBalanceDetailLambdaQueryWrapper.eq(CereBuyerBalanceDetail::getChangeType, param.getType());
        }
        cereBuyerBalanceDetailLambdaQueryWrapper.like(param.getDate() != null, CereBuyerBalanceDetail::getCreateTime, param.getDate());
        List<CereBuyerBalanceDetail> list = list(objectPage, cereBuyerBalanceDetailLambdaQueryWrapper);
        return R.ok(list);
    }
}
